/**

 *
 */

function soapWrapFunc(bdStr){
    // 封装SOAP头尾
    var soapHead = '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/"><env:Body>', soapTail = '</env:Body></env:Envelope>';
    return soapHead + bdStr + soapTail;
}

function loadMajorCode(){
	var str='<web:GetAllMajorInfo></web:GetAllMajorInfo>';
	$.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/MajorCodeService',
        data: soapWrapFunc(str),
        dataType: 'xml',
		success:function(xml){
			$.each($(xml).find('item'), function(i, n){
				$("#StuMajor").append("<option value="+$(this).find("ma_id").text()+">"+$(this).find("ma_majorName").text()+"</option>"); 
            
			});
			
		},
		beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
            alert('error:' + x.responseText);
        },
        complete: function(x){
        
        }
	});
}
//删除考场通过ID
function deleteRoomById(id){
	//验证报文
    var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
    var soapBody = '<web:DeleteRoomInfo>'+userinfo+'<id>' + id + '</id></web:DeleteRoomInfo>';
    $.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/RoomInfoService',
        data: soapWrapFunc(soapBody),
        dataType: 'xml',
        success: function(xml){
            alert($(xml).find('return').text());
			 loadRoomData();
        },
        beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
        	 var xmlObj = $(x.responseText);
             if (xmlObj.find('faultstring').text() == 'oops') {
                 alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                 return;
             }
             else {
                 alert('error:' + x.responseText);
             }
        },
        complete: function(x){
        
        }
    });
}
//删除考试，通过ID
function deleteExamById(id){
	//验证报文
    var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
    
    var soapBody = '<web:DeleteExamInfo>'+userinfo+'<id>' + id + '</id></web:DeleteExamInfo>';
    $.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/ExamService',
        data: soapWrapFunc(soapBody),
        dataType: 'xml',
        success: function(xml){
            alert($(xml).find('return').text());
			 loadExamData();
        },
        beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
        	var xmlObj = $(x.responseText);
            if (xmlObj.find('faultstring').text() == 'oops') {
                alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                return;
            }
            else {
                alert('error:' + x.responseText);
            }
        },
        complete: function(x){
        
        }
    });
}
//加载考场数据
function loadRoomData(){
	var str='<web:GetAllRoomInfo></web:GetAllRoomInfo>';
	    $.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/RoomInfoService',
        data: soapWrapFunc(str),
        dataType: 'xml',
        success: function(xml){
			var tableHtml = '<tbody>';
            $.each($(xml).find('item'), function(i, n){
				var num1,num2,num3;
				var ro_examPlace="",ro_stuType="",ro_faceOrPen="";
				num1=$(this).find('ro_examPlace').text();
				num2=$(this).find('ro_stuType').text();
				num3=$(this).find('ro_faceOrPen').text();
				if(num1=="0")
					ro_examPlace="杭州";
				else if(num1=="1")
					ro_examPlace="宁波";
				if(num2=="0")
					ro_stuType="自主考试";
				else if(num2=="1")
					ro_stuType="推荐选拔";
				else if(num2=="2")
					ro_stuType="考研调剂";
				if(num3=="0")
					ro_faceOrPen="笔试";
				else if(num3=="1")
					ro_faceOrPen="面试";
                tableHtml += (i % 2 == 0) ? '<tr>' : '<tr class="odd">';
                tableHtml += '<td>' +( i+1) + '<input type="checkbox"/></td>';
                tableHtml += '<td>' + $(this).find('ro_id').text() + '</td>';
                tableHtml += '<td>' + ro_examPlace+'<input type="hidden" value="'+num1+'"/>' + '</td>';
                tableHtml += '<td>' + ro_stuType+'<input type="hidden" value="'+num2+'"/>' + '</td>';
				tableHtml += '<td>' + $('#StuMajor option[value="'+$(this).find('ro_stuMajor').text()+'"]').text() +'<input type="hidden" value="'+$(this).find('ro_stuMajor').text()+'"/>'+ '</td>';
				tableHtml += '<td>' + ro_faceOrPen+'<input type="hidden" value="'+num3+'"/>' + '</td>';
                tableHtml += '<td><a href="javascript:void(0);" class="edit" >编辑</a><a href="javascript:void(0);" class="delete" >删除</a></td>';
                tableHtml += '</tr>';
            });
            tableHtml += '</tbody>';
            $('#roomInfoTable').find('tbody').remove().end().append(tableHtml);
			$('#roomInfoTable .edit').click(function(){
				$('#maskLayer').height($('#container').height()).show();
                $('#examWnd1').show();
                $('#wndTitle1 span').text('编辑考场信息');
                // 是编辑考试
                $.editMode = 1;
				var tdList=$(this).parents('tr').find('td');
				$.editID1 = tdList.eq(1).text();
				$('#RoomNum').val(tdList.eq(1).text());
				$('#ExamPlace').val(tdList.eq(2).find(':hidden').val());		
				$('#StuType').val(tdList.eq(3).find(':hidden').val());				
				$('#StuMajor').val(tdList.eq(4).find(':hidden').val());		
				$('#FaceOrPen').val(tdList.eq(5).find(':hidden').val());				
            });
            //删除按钮
            $('#roomInfoTable .delete').click(function(){
                $.editID1 = $(this).parents('tr').find('td').eq(1).text();
                deleteRoomById($.editID1);
            });
		},
		beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
            alert('error:' + x.responseText);
        },
        complete: function(x){
        
        }
		
		
    });
}
//加载考试数据
function loadExamData(){
	 var getExamStrBody = '<web:GetAllExamInfo></web:GetAllExamInfo>';
    
    $.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/ExamService',
        data: soapWrapFunc(getExamStrBody),
        dataType: 'xml',
        success: function(xml){
            var tableHtml = '<tbody>';
            $.each($(xml).find('item'), function(i, n){
                tableHtml += (i % 2 == 0) ? '<tr>' : '<tr class="odd">';
                tableHtml += '<td>' +( i+1) + '<input type="hidden" value="' + $(this).find('id').text() + '" /></td>';
                tableHtml += '<td>' + $(this).find('roomId').text() + '</td>';
                tableHtml += '<td>' +
                new String($(this).find('startTime').text()).replace('T', ' ').replace('+08:00', '') +
                '</td>';
                tableHtml += '<td>' + $(this).find('duration').text() + '</td>';
                tableHtml += '<td>' + $(this).find('subject').text() + '</td>';
                tableHtml += '<td><a href="javascript:void(0);" class="edit" >编辑</a><a href="javascript:void(0);" class="delete" >删除</a></td>';
                tableHtml += '</tr>';
            });
            tableHtml += '</tbody>';
            $('#examInfoTable').find('tbody').remove().end().append(tableHtml);
            //编辑按钮
            $('#examInfoTable .edit').click(function(){
				$('#maskLayer').height($('#container').height()).show();
                $('#examWnd').show();
                $('#wndTitle span').text('编辑考试信息');
                // 是编辑考试
                $.editMode = 1;
				var tdList=$(this).parents('tr').find('td'),dateTimeVal=tdList.eq(2).text();
                var dateReg = /\d{4}\-\d{2}\-\d{2}/,timeReg=/[0-2]{1}[0-6]{1}\:[0-5]{1}[0-9]{1}\:[0-5]{1}[0-9]{1}/;
				$.editID = tdList.eq(0).find(':hidden').val();
				$('#inClassRoomIndex').val(tdList.eq(1).text());	
				$('#inExamDate').val(dateTimeVal.match(dateReg));						
				var timeStr=new String(dateTimeVal.match(timeReg));
				$('#inExamTime_Hour').val(timeStr.split(':')[0]);
				$('#inExamTime_Min').val(timeStr.split(':')[1]);
				$('#inExamTime_Sec').val(timeStr.split(':')[2]);	
				$('#inDuration').val(tdList.eq(3).text());		
				$('#inSubjectName').val(tdList.eq(4).text());				
            });
            //删除按钮
            $('#examInfoTable .delete').click(function(){
                $.editID = $(this).parents('tr').find('td').eq(0).find(':hidden').val();
                deleteExamById($.editID);
            });
        },
        beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
            alert('error:' + x.responseText);
        },
        complete: function(x){
        
        }
    });
}

$(function(){
	$.editMode=0;
	loadMajorCode();
	loadRoomData();
    loadExamData();
});
/*
 *
 *
 */
$(window).load(function(){
	//拖拽绑定
	$('#examWnd').dragDrop({ focuEle: '#wndTitle' });
	$('#examWnd1').dragDrop({ focuEle: '#wndTitle1' });			
    $('.timezone').datePicker({
        startDate: '2000-01-01',
        endDate: '2100-01-01',
        clickInput: true,
        verticalOffset: 35
    });
	$('#closeBtn1').click(function(){
        $('#maskLayer').hide();
        $('#examWnd1').find('input[type="text"]').val('').end().hide();
    });
    $('#closeBtn').click(function(){
        $('#maskLayer').hide();
        $('#examWnd').find('input[type="text"]').val('').end().hide();
    });
	//新增考场
	$('#newRoom').click(function(){
        $('#maskLayer').height($('#container').height()).show();
        $('#examWnd1').show();
        $('#wndTitle1 span').text('新增考场信息');
        // 不是编辑考试，是新增考试
        $.editMode = 0;
    });
    // 新增考试
    $('#newButton').click(function(){
        $('#maskLayer').height($('#container').height()).show();
        $('#examWnd').show();
        $('#wndTitle span').text('新增考试信息');
        // 不是编辑考试，是新增考试
        $.editMode = 0;
    });
    // 文本框获得焦点的时候，显示TIP
    $('#examWnd1 input[type="text"]').focusin(function(){
        $(this).parent().prev('.tip').show();
    }).focusout(function(){
        $('#examWnd1 .tip').hide();
    });
    $('#examWnd input[type="text"]').focusin(function(){
        $(this).parent().prev('.tip').show();
    }).focusout(function(){
        $('#examWnd .tip').hide();
    });
	    $('#saveButton1').click(function(){
	    	//验证报文
	        var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';	
	    	
        var RoomNum = $.trim($('#RoomNum').val()), ExamPlace = $.trim($('#ExamPlace').val()), StuType = $.trim($('#StuType').val()), StuMajor = $.trim($('#StuMajor').val()), FaceOrPen = $.trim($('#FaceOrPen').val());
       
        /*
         * 这里要进行输入验证
         * 由大家完成
         * 正则或其他验证等等
         * 
         * 
         * 
         * 
         * 
         * 
         */
        var idValue, soapFuncName;
        //新增考试
        if ($.editMode == 0) {
            soapFuncName = 'NewRoomInfo';
            idValue = '';
        }
        else {
            //修改考试，需要多发送一个考试ID信息
            soapFuncName = 'ModifyRoomInfo';
            idValue = $.editID1;
        }
        var sendBody = '<web:' + soapFuncName + '>'+userinfo+'<roomEntry>' +

        '<ro_id>' +
        RoomNum +
        '</ro_id>' +
        '<ro_examPlace>' +
        ExamPlace +
        '</ro_examPlace>' +
        '<ro_stuType>' +
        StuType +
        '</ro_stuType>' +
        '<ro_stuMajor>' +
        StuMajor+
        '</ro_stuMajor>' +
        '<ro_faceOrPen>' +
        FaceOrPen +
        '</ro_faceOrPen>' +
        '</roomEntry></web:' +
        soapFuncName +
        '>';
        $.ajax({
            type: "POST",
            contentType: "text/xml",
            url: '../../Service/RoomInfoService',
            data: soapWrapFunc(sendBody),
            dataType: 'xml',
            success: function(xml){
                alert($(xml).find('return').text());
				 loadRoomData();
            },
            beforeSend: function(xhr){
                xhr.setRequestHeader("SOAPAction", "\"\"");
            },
            error: function(x, e){
            	 var xmlObj = $(x.responseText);
                 if (xmlObj.find('faultstring').text() == 'oops') {
                     alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                     return;
                 }
                 else {
                     alert('error:' + x.responseText);
                 }
            },
            complete: function(x){
            
            }
        });
    });
    $('#saveButton').click(function(){
    	//验证报文
        var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
        
        var classRoomIndexValue = $.trim($('#inClassRoomIndex').val()), examDateValue = $.trim($('#inExamDate').val()), durationValue = $.trim($('#inDuration').val()), subjectValue = $.trim($('#inSubjectName').val()), noteValue = $.trim($('#inNote').val());
        var hourValue = $.trim($('#inExamTime_Hour').val()), minValue = $.trim($('#inExamTime_Min').val()), secValue = $.trim($('#inExamTime_Sec').val());
        /*
         * 这里要进行输入验证
         * 由大家完成
         * 正则或其他验证等等
         * 
         * 
         * 
         * 
         * 
         * 
         */
        var idValue, soapFuncName;
        //新增考试
        if ($.editMode == 0) {
            soapFuncName = 'NewExamInfo';
            idValue = '';
        }
        else {
            //修改考试，需要多发送一个考试ID信息
            soapFuncName = 'ModifyExamInfo';
            idValue = $.editID;
        }
        var sendBody = '<web:' + soapFuncName + '>'+userinfo+'<examEntry>' +

        '<duration>' +
        durationValue +
        '</duration><id>' +
        idValue +
        '</id>' +
        '<note>' +
        noteValue +
        '</note>' +
        '<roomId>' +
        classRoomIndexValue +
        '</roomId>' +
        '<startTime>' +
        examDateValue +
        'T' +
        hourValue +
        ':' +
        minValue +
        ':' +
        secValue+
        '</startTime>' +
        '<subject>' +
        subjectValue +
        '</subject>' +
        '</examEntry></web:' +
        soapFuncName +
        '>';
        $.ajax({
            type: "POST",
            contentType: "text/xml",
            url: '../../Service/ExamService',
            data: soapWrapFunc(sendBody),
            dataType: 'xml',
            success: function(xml){
                alert($(xml).find('return').text());
				 loadExamData();
            },
            beforeSend: function(xhr){
                xhr.setRequestHeader("SOAPAction", "\"\"");
            },
            error: function(x, e){
            	 var xmlObj = $(x.responseText);
                 if (xmlObj.find('faultstring').text() == 'oops') {
                     alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                     return;
                 }
                 else {
                     alert('error:' + x.responseText);
                 }
            },
            complete: function(x){
            
            }
        });
    });
});
